﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace WinNMPForms
{
    internal class ProcessRun
    {
        /**
         * @param string exePath 程序执行绝对路径
         * @param string exeAgr 执行参数
         */
        internal static (string, string) ProcessStart(string exePath, string exeAgr)
        {
            (string, string) output = (null, null);

            try
            {
                using (Process myProcess = new Process())
                {
                    string fileName = "\"" + exePath + "\"";
                    string arguments =  exeAgr ;

                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.StartInfo.UseShellExecute = false;
                    myProcess.StartInfo.RedirectStandardInput = true;  // 重定向输入    
                    myProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出    
                    myProcess.StartInfo.RedirectStandardError = true;  // 重定向错误输出  
                    myProcess.StartInfo.FileName = fileName;
                    myProcess.StartInfo.Arguments = arguments;
                    myProcess.Start();
                    myProcess.WaitForExit();

                    // string so = myProcess.StandardOutput.ReadToEnd();
                    // string se = myProcess.StandardError.ReadToEnd();

                    output = (myProcess.StandardOutput.ReadToEnd(), myProcess.StandardError.ReadToEnd());

                    myProcess.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return output;
        }

        /**
         * @param string exeDir 程序执行工作目录
         * @param string exePath 程序执行名称
         * @param string exeAgr 执行参数
         */
        internal static (string, string) ProcessStart(string exeDir, string exeName, string exeAgr)
        {
            (string, string) output = (null, null);

            try
            {
                using (Process myProcess = new Process())
                {
                    string workingDirectory = exeDir;
                    string fileName = exeDir + exeName;
                    string arguments = exeAgr;

                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.StartInfo.UseShellExecute = false;
                    myProcess.StartInfo.RedirectStandardInput = true;  // 重定向输入    
                    myProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出    
                    myProcess.StartInfo.RedirectStandardError = true;  // 重定向错误输出  
                    myProcess.StartInfo.WorkingDirectory = workingDirectory;
                    myProcess.StartInfo.FileName = fileName;
                    myProcess.StartInfo.Arguments = arguments;
                    myProcess.Start();
                    myProcess.WaitForExit();

                    // string so = myProcess.StandardOutput.ReadToEnd();
                    // string se = myProcess.StandardError.ReadToEnd();

                    output = (myProcess.StandardOutput.ReadToEnd(), myProcess.StandardError.ReadToEnd());

                    myProcess.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return output;
        }

        /**
         * 关闭进程
         */
        public static void ProcessStop(string exeName)
        {
            Process[] myProcess = Process.GetProcessesByName(exeName);
            foreach (var process in myProcess)
            {
                process.Kill();
            }
        }

        public static void StartCmd(string cmdName, string examinerNo)
        {
            //进程的名称
            string fileName = "cmd.exe";

            string paraStart = cmdName + " " + examinerNo;

            //声明
            Process p = new Process();
            p.StartInfo.CreateNoWindow = true;         // 不创建新窗口    
            p.StartInfo.UseShellExecute = false;       //不启用shell启动进程  
            p.StartInfo.RedirectStandardInput = true;  // 重定向输入    
            p.StartInfo.RedirectStandardOutput = true; // 重定向标准输出    
            p.StartInfo.RedirectStandardError = true;  // 重定向错误输出  
            p.StartInfo.FileName = fileName;

            p.Start();

            // 执行并退出cmd
            p.StandardInput.WriteLine(paraStart + "&exit");
            p.StandardInput.AutoFlush = true;
            p.StandardInput.Close();
            p.WaitForExit();

            p.Close();
        }

        public static void NoWaitExitStartCmd(string exePath, string exeName, string examinerNo)
        {
            //进程的名称
            string fileName = "cmd.exe";
            // 盘符
            string driveLetter;
            if (exePath == null)
            {
                driveLetter = "C:";
            }
            else
            {
                driveLetter = exePath.Substring(0, 1);
            }

            // 目录
            string paraCd = "cd  \"" + exePath + "\"";
            string paraStart = exeName + " " + examinerNo;

            //声明
            Process p = new Process();
            p.StartInfo.CreateNoWindow = true;         // 不创建新窗口    
            p.StartInfo.UseShellExecute = false;       //不启用shell启动进程  
            p.StartInfo.RedirectStandardInput = true;  // 重定向输入    
            p.StartInfo.RedirectStandardOutput = true; // 重定向标准输出    
            p.StartInfo.RedirectStandardError = true;  // 重定向错误输出  
            p.StartInfo.FileName = fileName;

            p.Start();

            // 进入盘符
            p.StandardInput.WriteLine(driveLetter + ":");
            // 进入目录
            p.StandardInput.WriteLine(paraCd);
            // 执行并退出cmd
            p.StandardInput.WriteLine(paraStart + "&exit");
            p.StandardInput.AutoFlush = true;
            p.StandardInput.Close();

            p.Close();
        }
    }
}
